package aip.ui.message.srvc;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import aip.core.dao.information.CatalogDao;
import aip.core.dao.message.MessageDao;
import aip.core.dao.user.UserDao;
import aip.core.model.message.Message;
import aip.core.model.user.User;
import aip.core.mvc.pageController.PageController;
import aip.ui.message.web.spring.MessageEditForm;
import aip.ui.message.web.spring.MessageListForm;
import aip.ui.user.util.UserHelper;

import com.googlecode.horncomb.bean.paged.PagedList;
import com.googlecode.horncomb.bean.paged.PagedSetting;
import com.googlecode.horncomb.web.util.TagUtils;

/**
 * Message form service
 * <p>
 * 负责人/principal:<br/>
 * 修改记录/revision:<br/>
 * 日期:<br/>
 * 修改人:<br/>
 * 修改说明:<br/>
 * @author
 * @version $Id$
 */
public class MessageFormService
{
    private MessageDao messageDao;
    private CatalogDao catalogDao;
    private UserDao userDao;

    /**
     * Message列表
     * @param form
     *        表单对象
     * @return 列表页面数据
     * @throws Exception
     */
    public Map<String, Object> listReference(MessageListForm form)
            throws Exception
    {
        Map<String, Object> ref = new HashMap<String, Object>();
        PagedSetting ps = form.getPagedSettingMap().values().iterator().next();
        PagedList<List<? extends Message>> result = this.messageDao.query(ps
                .getConditionSet(), ps.getOrder(), ps.getPage(), ps
                .getPageSize());
        ref.put("messageList", result.getSource()); //$NON-NLS-1$
        ref.put("totalRows", result.getNrOfElements()); //$NON-NLS-1$

        return ref;
    }

    /**
     * 删除选中的项
     * @param form
     *        表单对象
     * @throws Exception
     */
    public void deleteItems(MessageListForm form) throws Exception
    {
        if (form.getSelectedIds().size() > 0)
        {
            this.messageDao.deleteByIds(form.getSelectedIds());
        }
    }

    /**
     * 获得Message表单主对象
     * @param form
     *        表单对象
     * @throws Exception
     */
    public void get(MessageEditForm form) throws Exception
    {
        if (form != null && form.getId() > 0)
        {
            Message message = this.messageDao.getById(form.getId());
            BeanUtils.copyProperties(message, form);
        }
    }

    /**
     * 显示编辑Message界面相关数据
     * @param form
     *        表单对象
     * @return 显示页面数据
     * @throws Exception
     */
    public Map<String, Object> showReference(MessageEditForm form)
            throws Exception
    {
        // other reference data
        Map<String, Object> ref = new HashMap<String, Object>();
        if (UserHelper.getUser() != null && form.getId() == 0)
        {
            form.setSubmiter(UserHelper.getUser().getUsername());
        }
        Set<String> userIds = new HashSet<String>();
        if (StringUtils.hasText(form.getExpert()))
        {
            userIds.add(form.getExpert());
        }
        if (StringUtils.hasText(form.getSubmiter()))
        {
            userIds.add(form.getSubmiter());
        }
        if (userIds.size() > 0)
        {
            ref.put("userMap", TagUtils.convertToMap(this.userDao
                    .queryListByIds(userIds.toArray(new String[] {})),
                    "username"));
        }
        ref.put("catalogMap", this.catalogDao.getCatalogMap());
        return ref;
    }

    /**
     * 保存Message表单
     * @param form
     *        表单对象
     * @throws Exception
     */
    public void save(MessageEditForm form) throws Exception
    {
        if (form.getId() == 0)
        {
            this.messageDao.insert(form);
        }
        else
        {
            this.messageDao.update(form);
        }
    }

    /**
     * 删除单个Message
     * @param form
     *        表单对象
     * @throws Exception
     */
    public void deleteItem(MessageEditForm form) throws Exception
    {
        this.messageDao.deleteById(form.getId());
    }

    /**
     * Message首面展现列表
     * @param form
     *        表单对象
     * @return 列表页面数据
     * @throws Exception
     */
    public Map<String, Object> messageIndexList(MessageEditForm form)
            throws Exception
    {
        Map<String, Object> ref = new HashMap<String, Object>();
        List<? extends Message> messageList = this.messageDao.queryListByType(
                form.getExpert(), form.getState(), form.getStart() + 1, 20);
        ref.put("messageList", messageList);
        String url = "../message/messageIndexList.shtml";
        if (StringUtils.hasText(form.getExpert())
                && StringUtils.hasText(form.getState()))
        {
            url.concat("?expert=").concat(form.getExpert()).concat("&state=")
                    .concat(form.getState());
        }
        // 分页
        if (messageList.size() > 0)
        {
            PageController pc = new PageController();
            pc.setPageSize(20);
            pc.setRowCount(this.messageDao.getDataCount(form.getExpert(), form
                    .getState()));
            pc.setShowPage(form.getStart());
            pc.setUrl(url);
            pc.setShowPageEnum(new Boolean(true));
            pc.setDivClassName("page");
            pc.setShowInput(new Boolean(true));
            pc.setShowPageEnumCount(5);
            pc.setButtonClassName("pageGoButton");
            pc.setInputClassName("pageInput");
            ref.put("pageStr", pc.getPageController());
            List<User> userList = this.userDao.queryListByIds(TagUtils
                    .parseIdString(messageList, "expert").toArray(
                            new String[] {}));
            ref.put("userMap", TagUtils.convertToMap(userList, "username"));
        }
        ref.put("catalogMap", this.catalogDao.getCatalogMap());
        return ref;
    }

    /**
     * @param messageDao
     *        the messageDao to set
     */
    public void setMessageDao(MessageDao messageDao)
    {
        this.messageDao = messageDao;
    }

    /**
     * @param catalogDao
     *        the catalogDao to set
     */
    public void setCatalogDao(CatalogDao catalogDao)
    {
        this.catalogDao = catalogDao;
    }

    /**
     * @param userDao
     *        the userDao to set
     */
    public void setUserDao(UserDao userDao)
    {
        this.userDao = userDao;
    }
}
